package com.banciyuan.bcywebview.biz.privatemessage;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.PrivateMessage;

/**
 * Created by squall on 2016/9/9.
 */
public class PrivateMessageAdapter extends BaseAdapter implements View.OnCreateContextMenuListener {

    private List<PrivateMessage> plist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private long unit_time = 5 * 60;


    public PrivateMessageAdapter(List<PrivateMessage> plist, Context mContext) {
        this.plist = plist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return plist.size();
    }

    @Override
    public Object getItem(int position) {
        return plist.get(plist.size() - 1 - position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        PrivateMessage message = (PrivateMessage) getItem(position);
        if (!StringUtil.notNullEqual(message.getUid(), UserDataHelper.getInstance().getUid())) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        try {
            menu.add(0, v.getId(), 0, mContext.getString(R.string.copy_paste));
            TextView textView = (TextView) v;
            ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("shareText", textView.getText().toString());
            clipboard.setPrimaryClip(clipData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class LeftHolder {
        private ImageView iv_header;
        private TextView tv_content;
        private TextView tv_time;

        public LeftHolder(View view) {
            iv_header = (ImageView) view.findViewById(R.id.header_iv);
            tv_content = (TextView) view.findViewById(R.id.left_text);
            tv_time = (TextView) view.findViewById(R.id.time_tv);
        }
    }

    private class RightHolder {
        private ImageView iv_header;
        private TextView tv_content;
        private TextView tv_time;

        public RightHolder(View view) {
            iv_header = (ImageView) view.findViewById(R.id.header_iv);
            tv_content = (TextView) view.findViewById(R.id.right_text);
            tv_time = (TextView) view.findViewById(R.id.time_tv);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LeftHolder leftHolder = null;
        RightHolder rightHolder = null;
        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = inflater.inflate(R.layout.talk_item_left, null);
                    leftHolder = new LeftHolder(convertView);
                    convertView.setTag(leftHolder);
                    break;
                case 1:
                    convertView = inflater.inflate(R.layout.talk_item_right, null);
                    rightHolder = new RightHolder(convertView);
                    convertView.setTag(rightHolder);
                    break;
            }
        } else {
            switch (getItemViewType(position)) {
                case 0:
                    leftHolder = (LeftHolder) convertView.getTag();
                    break;
                case 1:
                    rightHolder = (RightHolder) convertView.getTag();
                    break;
            }
        }

        final PrivateMessage p = (PrivateMessage) getItem(position);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        switch (getItemViewType(position)) {
            case 0:
                if (leftHolder != null) {
                    leftHolder.tv_content.setOnCreateContextMenuListener(this);
                    ImageLoader.getInstance().displayImage(p.getAvatar(), leftHolder.iv_header, ImageOptions.optionHead);
                    if (!TextUtils.isEmpty(p.getContent())) {
                        leftHolder.tv_content.setText(p.getContent());
                        MyLinkfy.addLinks(leftHolder.tv_content, MyRegex.plist.get(0), "bcyapp_");
                        UnderLineUtil.DisUnderLine(mContext, leftHolder.tv_content);
                    }
                    if (position - 1 > 0) {
                        PrivateMessage last = (PrivateMessage) getItem(position - 1);
                        if (p.getCtime() - last.getCtime() >= unit_time) {
                            leftHolder.tv_time.setVisibility(View.VISIBLE);
                            leftHolder.tv_time.setText(simpleDateFormat.format(p.getCtime() * 1000));
                        } else {
                            leftHolder.tv_time.setVisibility(View.GONE);
                        }
                    } else {
                        leftHolder.tv_time.setVisibility(View.VISIBLE);
                        leftHolder.tv_time.setText(simpleDateFormat.format(p.getCtime() * 1000));
                    }
                    leftHolder.iv_header.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, p.getUid());
                        }
                    });
                }
                break;
            case 1:
                if (rightHolder != null) {
                    rightHolder.tv_content.setOnCreateContextMenuListener(this);
                    ImageLoader.getInstance().displayImage(p.getAvatar(), rightHolder.iv_header, ImageOptions.optionHead);
                    if (!TextUtils.isEmpty(p.getContent())) {
                        rightHolder.tv_content.setText(p.getContent());
                        MyLinkfy.addLinks(rightHolder.tv_content, MyRegex.plist.get(0), "bcyapp_");
                        UnderLineUtil.DisUnderLine(mContext, rightHolder.tv_content);
                    }
                    if (position - 1 > 0) {
                        PrivateMessage last = (PrivateMessage) getItem(position - 1);
                        if (p.getCtime() - last.getCtime() >= unit_time) {
                            rightHolder.tv_time.setVisibility(View.VISIBLE);
                            rightHolder.tv_time.setText(simpleDateFormat.format(p.getCtime() * 1000));
                        } else {
                            rightHolder.tv_time.setVisibility(View.GONE);
                        }
                    } else {
                        rightHolder.tv_time.setVisibility(View.VISIBLE);
                        rightHolder.tv_time.setText(simpleDateFormat.format(p.getCtime() * 1000));
                    }
                    rightHolder.iv_header.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, p.getUid());
                        }
                    });
                }
                break;
        }
        return convertView;
    }
}
